package cn.ranko.shiro.controller;

import cn.ranko.bean.ComboBox;
import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.bean.TreeData;
import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysPermission;
import cn.ranko.shiro.model.SysRole;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.model.SysUserRole;
import cn.ranko.shiro.service.*;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by user on 2017/8/25.
 */
@Controller
@RequestMapping("/shiro/role")
public class RoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysMenuService sysMenuService;

    @RequiresPermissions("role")
    @RequestMapping("/index.action")
    public String index(ModelMap modelMap) {
        return "role/index";
    }

    @RequiresPermissions("role")
    @RequestMapping("/loadList.action")
    @ResponseBody
    public Datagrid loadList(Integer page, Integer rows) {
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setRows(rows);
        return sysRoleService.loadList(pagination);
    }

    @RequestMapping("/loadComboBoxData.action")
    @ResponseBody
    public List<ComboBox> loadComboBoxData(Integer userId) {
        Set roleset = sysUserRoleService.getSet(userId);

        List<ComboBox> comboBoxes = new ArrayList<>();
        List<SysRole> roles = sysRoleService.loadAll();
        for (SysRole role : roles) {
            ComboBox comboBox = new ComboBox();
            comboBox.setId(role.getRoleId());
            comboBox.setText(role.getRolename());
            if (roleset.contains(role.getRoleId())) {
                comboBox.setSelected(true);
            }
            comboBoxes.add(comboBox);
        }
        return comboBoxes;
    }

    @RequestMapping("/loadTreeData.action")
    @ResponseBody
    public List<TreeData> loadTreeData() {
        List<TreeData> treeDatas = new ArrayList<>();
        treeDatas = sysMenuService.loadTreeData();
        return treeDatas;
    }

    @RequiresPermissions(value = {"role_add","role_edit"},logical = Logical.OR)
    @RequestMapping("/showedit.action")
    public String showedit(Integer roleId, ModelMap modelMap) {

        if(roleId != 0){
            SysRole role = sysRoleService.getRole(roleId);
            modelMap.addAttribute("role", role);
        }

        return "role/showedit";
    }

    /**
     * 编辑角色
     *
     * @return
     */
    @RequiresPermissions(value = {"role_add","role_edit"},logical = Logical.OR)
    @RequestMapping("/editRole.action")
    @ResponseBody
    public String editRole(SysRole role, String permissionstrs) {

        if (role.getRoleId() == 0) {
            //添加
            sysRoleService.insertRole(role);

            //进行insert操作
            String[] splits = permissionstrs.split(",");
            sysPermissionService.insertBatch(role, splits);

            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(), ApplicationCode.SUCCESS.getMessage()));
        } else {
            //修改
            sysRoleService.updateRole(role);

            sysPermissionService.deletePermission(role.getRoleId());
            //进行insert操作
            String[] splits = permissionstrs.split(",");
            sysPermissionService.insertBatch(role, splits);

            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(), ApplicationCode.SUCCESS.getMessage()));
        }
    }

    /**
     * 删除角色
     *
     * @return
     */
    @RequiresPermissions("role_disable")
    @RequestMapping("/deleteRole.action")
    @ResponseBody
    public String deleteRole(Integer roleId) {
        Integer affectrows = sysRoleService.deleteRole(roleId);
        if (affectrows > 0) {
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(), ApplicationCode.SUCCESS.getMessage()));
        } else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(), ApplicationCode.ERROR.getMessage()));
        }
    }


}
